package com.qiyun.duijiang.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Futil {

    private static ArrayList<Double> tmpArr = new ArrayList<>();
    private static Double d = 0.0;

    public static Double authUp(Map<Integer,Double> map,String strand){
        List list = new ArrayList(map.values());
        Double[] oddses = (Double[])list.toArray(new Double[list.size()]);
        Double winauth = 0.0;
        if (strand.contains("单关")){
            for (int i = 0; i < oddses.length; i++) {
                winauth = winauth + oddses[i];
            }
            return winauth*2;
        }
        if(strand.contains("2串1")&&map.size()>=2){
            combine(0,2,oddses);
            winauth = winauth+d;
            d = 0.0;
        }if(strand.contains("3串1")&&map.size()>=3){
            combine(0,3,oddses);
            winauth = winauth+d;
            d = 0.0;
        }if(strand.contains("4串1")&&map.size()>=4){
            combine(0,4,oddses);
            winauth = winauth+d;
            d = 0.0;
        }if(strand.contains("5串1")&&map.size()>=5){
            combine(0,5,oddses);
            winauth = winauth+d;
            d = 0.0;
        }if(strand.contains("6串1")&&map.size()>=6){
            combine(0,6,oddses);
            winauth = winauth+d;
            d = 0.0;
        }if(strand.contains("7串1")&&map.size()>=7){
            combine(0,7,oddses);
            winauth = winauth+d;
            d = 0.0;
        }if(strand.contains("8串1")&&map.size()==8){
            combine(0,8,oddses);
            winauth = d;
            d = 0.0;
        }
        return winauth*2;
    }
    /**
     * 组合
     * 按一定的顺序取出元素，就是组合,元素个数[C arr.len 3]
     * @param index 元素位置
     * @param k 选取的元素个数
     * @param arr 数组
     */
    public static void combine(int index,int k,Double[] arr) {
        if(k == 1){
            for (int i = index; i < arr.length; i++) {
                tmpArr.add(arr[i]);
                d = d + answer(tmpArr);
                tmpArr.remove(arr[i]);
            }
        }else if(k > 1){
            for (int i = index; i <= arr.length - k; i++) {
                tmpArr.add(arr[i]); //tmpArr都是临时性存储一下
                combine(i + 1,k - 1, arr); //索引右移，内部循环，自然排除已经选择的元素
                tmpArr.remove(arr[i]); //tmpArr因为是临时存储的，上一个组合找出后就该释放空间，存储下一个元素继续拼接组合了
            }
        }else {
            return ;
        }
    }

    public static Double answer(ArrayList<Double> tmpArr){
        Double d = 0.0;
        for (int i = 0; i <tmpArr.size() ; i++) {
            if (i==0){
                d = tmpArr.get(i);
            }else {
                d = d * tmpArr.get(i);
            }
        }
        return d;
    }

}
